home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12226 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Bad code
  5. Date: 29 Mar 1996 14:00:25 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4jhmhpINNps7@anvil.ugrad.cs.ubc.ca>
  8. References: <4jhau8$hrl@hermes.oanet.com>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10. Keywords: Problems
  11.  
  12. In article <4jhau8$hrl@hermes.oanet.com>,
  13.  <scorpion@portal.connect.ab.ca> wrote:
  14. >    I need to know why a piece of my C code doesn't work . 
  15. >it does like:
  16. >
  17. >    Object.Vertices+i.X = (long) .....
  18. >    Object.Vertices+i.Y = (long).......
  19. >    Object.Vertices+i.Z=(long) ........
  20. >
  21. >    Vertices is a pointer to a struct with {long X,Y,Z} that 
  22. >has been typedef'ed. i is an index vatiable that is added to the 
  23. >pointer to make is point to the next {long X,Y,Z} typedef'ed 
  24. >struct . When I go to compile , it says "not a struct or union 
  25. >type".
  26.  
  27. Object.Vertices is NOT a struct or union type, so you can't use the '.'
  28. operator to access members in it. It is a pointer to a structure. Therefore you
  29. have to dereference the pointer before accessing a member:
  30.  
  31.     (*Object.Vertices+i).x = (long) ....
  32.  
  33. This is a bit clumsy, which is why most programmers use the following
  34. equivalent notation:
  35.  
  36.     (Object.Vertices+i)->X = ...
  37.  
  38.  
  39. When I parenthesize Vertices+i , I get an "Identified 
  40.  
  41. That's because (Vertices+i) is not a member of Object structure. It is a
  42. pointer expression. You cannot write Object.(Vertices + i). An identifier is
  43. expected after the ``Object.'' to name the member.
  44.  
  45.  
  46. You _must_ parenthesize it as I did above, because  the + operator has a low
  47. precedence compared to . or ->. If you don't, the compiler will think you are
  48. trying to do:
  49.  
  50.     (Object.Vertices) + (i->X)
  51.  
  52. Which is illegal, since i is (presumably) an integer and not a pointer to a
  53. structure.
  54. -- 
  55.  
  56.